package com.dems.admin.controller;

import com.dems.domain.JsonResponse;
import com.dems.domain.ftp.FtpConfigData;
import com.dems.service.util.FtpUtil;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

@RestController
@RequestMapping("/file")
public class FtpFileController {

    @Value("${ftp.endpoint}")
    private String endpoint;

    @Value("${ftp.filePath}")
    private String filePath;

    @Value("${ftp.userName}")
    private String ftpUserName;

    @Value("${ftp.password}")
    private String ftpPassword;

    @PostMapping("/upload")
    public JsonResponse uploadFile(@RequestBody FtpConfigData ftpConfigData) {
        InputStream stream = null;
        ftpConfigData.setEndpoint(this.endpoint);
        ftpConfigData.setUserName(this.ftpUserName);
        ftpConfigData.setPassword(this.ftpPassword);
        ftpConfigData.setUpLoadPath(this.filePath);
        FtpUtil ftpUtil = new FtpUtil(ftpConfigData);
        String fileName = ftpConfigData.getFileName() + "_" + System.currentTimeMillis() + "." + ftpConfigData.getFileType();
        try {
            byte[] bt = Base64.getDecoder().decode(ftpConfigData.getBase64Content());
            stream = new ByteArrayInputStream(bt);
            String ftpFilePath = ftpConfigData.getUpLoadPath() + fileName;
            ftpUtil.uploadFile(ftpFilePath, stream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonResponse.success(fileName);
    }

    @PostMapping("/download")
    public void downloadFile(HttpServletResponse response, @RequestBody FtpConfigData ftpConfigData) {
        ftpConfigData.setEndpoint(this.endpoint);
        ftpConfigData.setUserName(this.ftpUserName);
        ftpConfigData.setPassword(this.ftpPassword);
        ftpConfigData.setUpLoadPath(this.filePath);
        InputStream inputStream = null;
        try {
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding(StandardCharsets.UTF_8.name());
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(ftpConfigData.getFileName(), "UTF-8"));
            FtpUtil ftpUtil = new FtpUtil(ftpConfigData);
            inputStream = ftpUtil.downLoadFile(filePath+ftpConfigData.getFileName());
            response.addHeader("Content-Length", "" + inputStream.available());
            IOUtils.copy(inputStream, response.getOutputStream());
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
